// page-deviceInformationImg/home.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: 'https://www.gcjxglzx.com/upload/uploadImgTest',
    path: '',//文件本地路径
    name: '',//name标识符
    pathType: '',//自定义上传的目录路径
    identityNum:'',//身份证号
    zhengchesfz: app.globalData.registerImage[7], //7整车和身份证照
    namePlate: app.globalData.registerImage[13], //13整车铭牌和身份证照
    dajiazisfz: app.globalData.registerImage[14], //14大架子（出厂编号）和身份证照
    engine: app.globalData.registerImage[15], //15发动机铭牌照
    yeyabeng: app.globalData.registerImage[16], //16液压泵铭牌照
    bigShelfNum: '',//出厂编号
    qylxr: false
  },
  chooseImage: function (e) {
    console.log(e.target.id)
    var self = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'],
      success: function (res) {
        if (e.target.id == '0') {
          self.setData({
            name: "zhengchesfz"
          });
          self.setData({
            zhengchesfz: res.tempFilePaths,
            path: res.tempFilePaths,
            pathType: self.data.identityNum + "/" + self.data.bigShelfNum +'/' + self.data.name
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            self.setData({
              zhengchesfz: imgResponseUrl
            })
          };
        } else if (e.target.id == '1') {
          self.setData({
            name: "namePlate"
          });
          self.setData({
            namePlate: res.tempFilePaths,
            path: res.tempFilePaths,
            pathType: self.data.identityNum + "/" + self.data.bigShelfNum + '/' + self.data.name
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            self.setData({
              namePlate: imgResponseUrl
            })
          };
        } else if (e.target.id == '2') {
          self.setData({
            name: "dajiazisfz"
          });
          self.setData({
            dajiazisfz: res.tempFilePaths,
            path: res.tempFilePaths,
            pathType: self.data.identityNum + "/" + self.data.bigShelfNum + '/' + self.data.name
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            self.setData({
              dajiazisfz: imgResponseUrl
            })
          };
        } else if (e.target.id == '3') {
          self.setData({
            name: "engine"
          });
          self.setData({
            engine: res.tempFilePaths,
            path: res.tempFilePaths,
            pathType: self.data.identityNum + "/" + self.data.bigShelfNum + '/' + self.data.name
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            self.setData({
              engine: imgResponseUrl
            })
          };
        } else if (e.target.id == '4') {
          self.setData({
            name: "yeyabeng"
          });
          self.setData({
            yeyabeng: res.tempFilePaths,
            path: res.tempFilePaths,
            pathType: self.data.identityNum + "/" + self.data.bigShelfNum + '/' + self.data.name
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            self.setData({
              yeyabeng: imgResponseUrl
            })
          };
        }
      }
    });
  },
  submit: function (e) {
    var that = this;
    var bigShelfNum = that.data.bigShelfNum;//设备编号
    var identityNum = that.data.identityNum;//身份证号
    var identityAndCarImg = that.data.zhengchesfz; //整车和身份证照
    var identityAndPlateImg = that.data.namePlate; //整车铭牌和身份证照
    var identityAndBigShelfImg = that.data.dajiazisfz; //大架子号和身份证照
    var enginePlateImg = that.data.engine; //发动机铭牌照
    var hydraulicPlateImg = that.data.yeyabeng;//液压泵铭牌照
    wx.request({
      url: 'https://www.gcjxglzx.com/deviceInformationImg/save',
      method: 'POST',
      data: {
        bigShelfNum: bigShelfNum,
        identityNum: identityNum,
        identityAndCarImg: identityAndCarImg,
        identityAndPlateImg: identityAndPlateImg,
        identityAndBigShelfImg: identityAndBigShelfImg,
        enginePlateImg: enginePlateImg,
        hydraulicPlateImg: hydraulicPlateImg 
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //将该设备添加入上户表
        if (res.data.status == '0') {
          wx.navigateTo({
            url: '/page-deviceImg/home',
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      bigShelfNum: wx.getStorageSync("factoryNum"),
      identityNum: wx.getStorageSync('identification')
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})